package com.soilflow.info.service.impl;

import com.soilflow.common.core.domain.entity.SysUser;
import com.soilflow.info.service.MessageService;
import com.soilflow.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplate;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private ISysUserService userService;
    @Autowired
    private WxMpService wxMpService;

    /**
     * 公众号推送成功消息
     * @param userId
     */
    @Override
    public void pushPendingMessage(Long userId) {
        SysUser user = userService.selectUserById(userId);
        String openId = null;
        //方便测试，给默认值（开发者本人的openId）
        if(StringUtils.isEmpty(openId)) {
            openId = "oDGui6lst6Zh1nAIJnPp7k7G7bOc";
        }
        //设置消息发送信息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(openId) //给谁发送信息 人的openid
                .templateId("wknQIUT8pF0I8l29IqkNb5cj66lM7vo3jYtcWvYxybo") //模板id
                .build();
        //设置模板里面参数值
        templateMessage.addData(new WxMpTemplateData("first","土流网微信小程序"));
        templateMessage.addData(new WxMpTemplateData("keyword1","金桂c区"));
        templateMessage.addData(new WxMpTemplateData("keyword2",new DateTime().toString("yyyy-MM-dd HH:mm:ss")));
        templateMessage.addData(new WxMpTemplateData("keyword3","wy"));
        templateMessage.addData(new WxMpTemplateData("content", "请注意提醒", "#272727"));
        //调用方法
        try {
            String msg = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            System.out.println("msg = " + msg);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }


    }
}
